<?php


namespace app\websocket\model;


use think\Model;

class CustomerOrder extends Model
{
    /**
     * getCustomerOrderSales
     * 获取当天销售数据
     * @url
     * @method
     * @param int $user 用户id
     * @return int
     * @author Asira
     * @date 2021/2/3
     */
    public static function getCustomerOrderSales($user): int
    {
        $OrderCustomer=new self();
        $result=ActionRole::checkUserActionRole($user,'order');
        if($result===false){
            return false;
        }
        ['search_user'=>$search_user,'group'=>$group]=$result;

        if(!empty($search_user)){
            $where['create_user']=['=',$search_user];
        }
        if(!empty($group)){
            $where['create_group']=['=',$group];
        }

        $where['del_flag']=['=',0];
        $date=date('Y-m-d');
        $where['create_time']=['between',[$date.' 00:00:00',$date.' 23:59:59']];

        $obj=$OrderCustomer
            ->where($where)
            ->sum('selling_price');
        if(empty($obj)){
            $sales_data=0;
        }else{
            $sales_data=round($obj,2);
        }

        return $sales_data;
    }

    /**
     * getCustomerSalesPolyline
     * 获取最近销售额柱状图数据
     * @url
     * @method
     * @param int $user 用户id
     * @return array|bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author Asira
     * @date 2021/2/3
     */
    public static function getCustomerSalesPolyline($user){
        $OrderCustomerClass=new self();

        $result=ActionRole::checkUserActionRole($user,'order');
        if($result===false){
            return false;
        }
        ['search_user'=>$search_user,'group'=>$group]=$result;
        if(!empty($search_user)){
            $where['create_user']=['=',$search_user];
        }
        if(!empty($group)){
            $where['create_group']=['=',$group];
        }
        $days=6;
        for($i=$days;$i>0;$i--){
            $day_label[]=date('m-d',strtotime("-$i day"));
        }
        $day_label[]= date('m-d');
        $date_start=date('Y-m-d',strtotime("-$days day"));
        $date_end=date('Y-m-d');

        $where['del_flag']=['=',0];
        $where['create_time']=['between',[$date_start.' 00:00:00',$date_end.' 23:59:59']];

        $data=$OrderCustomerClass
            ->where($where)
            ->field('sum(selling_price) price,date_format(create_time,\'%m-%d\') days')
            ->group('days')
            ->select()->toArray();
        $polyline_data=['type'=>'line','title'=>['text'=>'近 '.($days+1).' 天销售额情况'],'xRorate'=>0,'labels'=>$day_label];
        $total_sales=['label' => "总额",'data'=>[]];
        if(empty($data)){
            $total_sales['data']=[0,0,0,0,0];
        }else{
            $order_data=[];
            foreach ($data as $key=>$value){
                $order_data[$value['days']]=['price'=>round($value['price'],2)];
            }
            foreach ($day_label as $key=>$value){
                $total_sales['data'][]=$order_data[$value]['price']??0;
            }
        }
        $polyline_data['datasets']=[$total_sales];
        return $polyline_data;
    }
}